<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/13 14:58
 * Desc: 美食
 */
class Controller_Pc_Api_V2_Food extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Food
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Food
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('food', $this->request_params);

        // 服务
        $this->service = new Service_Api_Food();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 搜索条件
    public function action_get_query_condition()
    {
        try {
            $this->result['data'] = $this->service->query_condition();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 列表
    public function action_query()
    {
        try {
            $this->validation->query()->check();

            $this->result['data'] = $this->service->search($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 二级类目列表
    public function action_second_category()
    {
        try {
            $this->validation->second_category()->check();

            $this->result['data'] = $this->service->second_category($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 获取产品数据详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $this->result['data'] = $this->service->info($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 创建订单
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->create_order($this->request_params);
        } catch (Exception $e) {
            $this->result['msg']    = $e->getMessage();
            $this->result['status'] = false;
        }
    }


    // 获取首页类目
    public function action_index_attr()
    {
        try {
            $this->validation->index_attr()->check();

            $this->result['data'] = $this->service->index_attr($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取推荐商家列表
    public function action_index_recommend()
    {
        try {
            $this->validation->query()->check();

//            $this->request_params['sort_type'] = 3;
            $this->result['data']              = $this->service->search($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 获取套餐数据详情
    public function action_get_suit_detail()
    {
        try {
            $this->validation->get_suit_detail()->check();

            $this->result['data'] = $this->service->get_suit_detail($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 创建买单订单
    public function action_add_discount_order()
    {
        try {
            $this->validation->add_discount_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->add_discount_order($this->request_params);
        } catch (Exception $e) {
            $this->result['msg']    = $e->getMessage();
            $this->result['status'] = false;
        }
    }

}